package com.dc.common.aop;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dc.base.utils.RequestUtil;
import com.dc.system.model.SysOperationlog;
//import com.dc.system.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.dc.common.constants.ConstantsCommon;
import com.dc.common.model.BaseModel;
import com.dc.system.model.SysUser;

/**
 * 日志记录切面
 *
 * @author Shen
 * @date 2018年4月10日 下午1:26:29
 */
@Component
@Aspect
public class LogAspect {

    /*@Autowired
    private LogService logService;

    @Pointcut("@annotation(com.dc.common.aop.LogAop)")
    public void logOperationAspect() {
    }

    @After("logOperationAspect() && @annotation(data)")
    public void doAfter(JoinPoint point, LogAop data) throws Exception{
        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();

        // 获取baseModel
        BaseModel baseModel = new BaseModel();
        if (point.getArgs() != null) {
            for (Object obj : point.getArgs()) {
                if (obj != null && obj.getClass() == BaseModel.class) {
                    baseModel = (BaseModel) obj;
                    break;
                }
            }
        }
        // 放入操作日志
        SysOperationlog systemOperationLog = new SysOperationlog();
        SysUser systemUser = (SysUser) session.getAttribute(ConstantsCommon.LOGIN_INFO);
        systemOperationLog.setContent(baseModel.getAopMsg());
        if (systemUser != null) {
            systemOperationLog.setUserId(systemUser.getUserId());
            systemOperationLog.setUserName(systemUser.getName());
            systemOperationLog.setUserAccount(systemUser.getAccount());
        } else {
            systemOperationLog.setContent("无登陆用户，请检查是否是接口调试，还是被恶意调用");
        }

        systemOperationLog.setDate(new Date());
        systemOperationLog.setType(data.type());
        systemOperationLog.setModule(data.module().getModule());
        systemOperationLog.setRequestIp(RequestUtil.getInstance().getIpAddress(request));
        systemOperationLog.setRequestMethod(
                point.getTarget().getClass().getName() + "." + point.getSignature().getName() + "()");

        //参数可能太多，暂时不保存
//		Map<String, String[]> pramMap = request.getParameterMap();
//		String msg = JSONObject.toJSONString(pramMap);
//      systemOperationLog.setRequest_params(msg);
//      systemOperationLog.setParam_before(baseModel.getDataBefore());
//      systemOperationLog.setParam_after(baseModel.getDataAfter());
        logService.insertOperationLog(baseModel,systemOperationLog);
    }*/

}
